﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.DB;
using MaxLeaf.Common;
using System.Data;

namespace MaxLeaf.DAL.K3
{
    //insert t_barcode(FTypeID,FItemID,FBarcode,FRemark) 
    //values(4,44739,'123','test')

    /// <summary>
    /// 条形码
    /// </summary>
    public class t_barcodeDAL : BaseDAL
    {
        public t_barcodeDAL()
            : base()
        {
            strTableName = "t_barcode";

            strMapKeyFieldName = "Fid";
            strDestRealKeyFieldName = "FBarCode";
             
            mblnNeedEditKeyValue = false;
            mblnIsIdentity = true;
            strFieldNames = "FTypeID,FItemID,FBarcode,FRemark";
        }
        public override void Update(DBAccess dbOrgCon, DBAccess dbDestCon, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", strTableName }, { "OrgKeyValue", strKeyValue } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFid = dbDestCon.GetValue(strSql, kvp.Value);
            if (strFid.Length > 0)
            {

            }
            else
            {
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow(strTableName, new Dictionary<string, object>() { { strMapKeyFieldName, strKeyValue } });
                dictOtherKeyFieldsCond = new Dictionary<string, object>() { 
                    {"FTypeID",4}
                    ,{"FItemID",drOrgData["FItemID"]}
                };
                dictDestOtherKeyFieldsCond = new Dictionary<string, object>() { 
                    {"FTypeID",4}
                    ,{"FItemID",new ImInvMasterDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FItemID"]))}
                };
            }
            base.Update(dbOrgCon, dbDestCon, strKeyValue);
        }
        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FItemID", StringComparison.InvariantCultureIgnoreCase))
            {
                //物料
                drDestData[strFieldName] = GetDestFItemID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FItemID"]));

            }

            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetDestFItemID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFItemID)
        { 
            ImInvMasterDAL dal=new ImInvMasterDAL();
            return dal.GetDestMapKeyValue(dbOrgCon, dbDestCon, strOrgFItemID);
            
        }

    }
}
